<script>
import {createProps} from '@/utils';
import {createFormDefinition} from '@comp/hhd/design/FormDesigner/types';

export default {
  name: 'RelationColumnRender',
  props: {
    columnScope: createProps(Object, () => undefined),
    formDefinition: createProps(Object, createFormDefinition),
    columns: createProps(Array, () => []),
  },
  render(h) {
    let result = this.tryRender();
    return result || h('span', '');
  },
  methods: {
    tryRender() {
      try {
        let columns = this.columns || [];
        let column = this.columnScope.column;
        let fieldConfig = columns.find(c => c.prop == column.property).fieldConfig;
        return this.renderRowField(fieldConfig, this.columnScope.row);
      } catch (e) {
        console.log(e);
      }
    },
    renderRowField(field, row) {
      let h = this.$createElement;
      if (field.fieldTitleName) {
        return h('span', row[`${field.id}&title`]);
      }
      return h('span', row[field.id]);
    },
  }
};
</script>